package tcp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Mark
 * @version 1.0
 * @date 2020/5/17 17:47
 * @description 服务端接收文件
 */
public class FileUploadServer {
    public static void main(String[] args) throws Exception {

        // 1.创建服务
        ServerSocket serverSocket = new ServerSocket(9000);

        // 2.监听
        Socket accept = serverSocket.accept();

        // 3.获取输入流
        InputStream inputStream = accept.getInputStream();

        // 4.文件输出
        FileOutputStream fileOutputStream = new FileOutputStream(new File("file"));
        byte[] bytes = new byte[1024];
        int length;
        while ((length = inputStream.read(bytes)) != -1) {
            fileOutputStream.write(bytes, 0, length);
        }
        OutputStream outputStream = accept.getOutputStream();
        outputStream.write("over".getBytes());

        // 5.关闭流
        fileOutputStream.close();
        inputStream.close();
        accept.close();
        serverSocket.close();
    }
}
